package net.ecpt.crm.services;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.poi.EncryptedDocumentException;
import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.UtilDateTime;
import org.ofbiz.base.util.UtilMisc;
import org.ofbiz.base.util.UtilValidate;
import org.ofbiz.entity.Delegator;
import org.ofbiz.entity.GenericEntityException;
import org.ofbiz.entity.GenericValue;
import org.ofbiz.entity.util.EntityUtil;
import org.ofbiz.service.DispatchContext;
import org.ofbiz.service.ServiceUtil;
import org.ofbiz.wvtools.utils.ExcelPoiUtils;

import javolution.util.FastList;

public class CrmReceivablesXlsServices {
    public static final String module = CrmReceivablesXlsServices.class.getName();

    /**
     * 财务导入回款记录，根据回款编号可以再次修改
     */
    @SuppressWarnings("unchecked")
    public static Map<String, Object> importLiveupReceivablesXls(DispatchContext dctx, Map<String, ? extends Object> context) {
	Map<String, Object> result = ServiceUtil.returnSuccess();
	Delegator delegator = dctx.getDelegator();
	GenericValue userLogin = (GenericValue)context.get("userLogin");
	List<String> messageList = new ArrayList<String>();
	List<Map<String, Object>> sheetList = (List<Map<String, Object>>) context.get("sheetList");
	try {
	    int i = 0;
	    List<GenericValue> entityList = FastList.newInstance();
	    for (Map<String, Object> row : sheetList) {
		i = i + 1;
		String receivableCode = ExcelPoiUtils.getCellString(row, 0);//回款编号
		String contractCode = ExcelPoiUtils.getCellString(row, 1);//合同编号
		Timestamp returnTime = ExcelPoiUtils.getCellTimestamp(row, 2);//回款日期
		
		String returnType = ExcelPoiUtils.getCellString(row, 3);//回款方式
		BigDecimal money = ExcelPoiUtils.getCellBigDecimal(row, 4);//回款金额
		
        
		if (UtilValidate.isEmpty(receivableCode)) {
		    messageList.add("第" + i + "行,回款编号不能为空。");
		    continue;
		}
		if (UtilValidate.isEmpty(contractCode)) {
		    messageList.add("第" + i + "行,合同编号不能为空。");
		    continue;
		}
		if (UtilValidate.isEmpty(returnTime)) {
		    messageList.add("第" + i + "行,回款日期不能为空。");
		    continue;
		}
		if (UtilValidate.isEmpty(returnType)) {
		    messageList.add("第" + i + "行,回款方式不能为空。");
		    continue;
		}
		if (UtilValidate.isEmpty(money)) {
		    messageList.add("第" + i + "行,回款金额不能为空。");
		    continue;
		}
		
		
		
       List<GenericValue> oldCrmReceivablesInfo = delegator.findByAnd("CrmReceivables",UtilMisc.toMap("receivableCode", receivableCode),UtilMisc.toList("id"),false);
		
		GenericValue oldCrmReceivables =EntityUtil.getFirst(oldCrmReceivablesInfo);
		
		GenericValue entity=oldCrmReceivables;
		if(UtilValidate.isEmpty(oldCrmReceivables)) {
			String id = GenerateIdUtils.getId();
			entity = delegator.makeValue("CrmReceivables",UtilMisc.toMap("id", id));
		}
		
		
		entity.set("receivableCode", receivableCode);
		
		
        List<GenericValue> crmContractInfo = delegator.findByAnd("CrmContract",UtilMisc.toMap("contractCode", contractCode),UtilMisc.toList("id"),false);
		
		GenericValue crmContract =EntityUtil.getFirst(crmContractInfo);
		
		if (UtilValidate.isEmpty(crmContract)) {
		    messageList.add("第" + i + "行,合同编号错误,不能查找到合同。");
		    continue;
		}
		entity.set("contractId", crmContract.getString("id"));
		entity.set("customerId", crmContract.getString("customerId"));
		entity.set("ownerUserId", crmContract.getString("ownerUserId"));
		
		
		
		entity.set("returnTime", returnTime);
		entity.set("returnType", returnType);
		entity.set("money", money);
		
		
		
		entity.set("createTime", UtilDateTime.nowTimestamp());

		entity.set("createUserId", userLogin.getString("partyId"));
		entityList.add(entity);
	
	
	    }
	    delegator.storeAll(entityList);
	    result.put("messageList", messageList);
	} catch (EncryptedDocumentException e) {
		Debug.logError(e.getMessage(), module);
	} catch (GenericEntityException e) {
		Debug.logError(e.getMessage(), module);
		}
	return result;
    }
    
    
}
